package config

import (
	"io/ioutil"

	log "../lib/log"
	toml "github.com/pelletier/go-toml"
)

// Config standard config params
type Config struct {

	//rpc server
	ServerRPCPort string

	// postgresql database
	PostgresHost string

	// basic path
	AssertBasicPath string
}

// configs
var (
	ServerRPCPort   string
	PostgresHost    string
	AssertBasicPath string
)

func get(tree *toml.Tree, param string) string {
	res := tree.Get(param)
	if res == nil {
		log.Fatal(param, " in toml file is invailed, plz check the toml file.")
	}
	return res.(string)
}

//Load load config file
func Load(path string) {
	dat, err := ioutil.ReadFile(path)
	if err != nil {
		log.Fatal(err)
	}
	configs, err := toml.Load(string(dat))
	if err != nil {
		log.Fatal(err)
	}

	ServerRPCPort = get(configs, "server.rpc_port")
	PostgresHost = get(configs, "postgres.host")
	AssertBasicPath = get(configs, "assert.basic_path")

	log.Info("Got config below:")
	log.Info("ServerRPCPort: ", ServerRPCPort)
	log.Info("PostgresHost:", PostgresHost)
	log.Info("AssertBasicPath: ", AssertBasicPath)
}
